﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class PunishmentTypeGateway:DBGateway
    {
        public bool HasThisTypeExist(PunishmentType punishmentType)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select Type from tblPunishmentType where Type='"+punishmentType.Type+"'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw new Exception("Error occured during Reading Information");
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public string SavePunishedType(PunishmentType punishmentType)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblPunishmentType values(@type)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@type", SqlDbType.VarChar).Value = punishmentType.Type;
                CommandObj.ExecuteNonQuery();
                return "Punishment type Saved Successfully";
            }
            catch (Exception ex)
            {
                throw new Exception("Error occured during Saving Punishment Type");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public List<PunishmentType> GetAllPunishmentType()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblPunishmentType";
                SqlDataReader reader = CommandObj.ExecuteReader();
                List<PunishmentType>punishmentTypes=new List<PunishmentType>();
                while (reader.Read())
                {
                    PunishmentType punishment=new PunishmentType();
                    punishment.Id = Convert.ToInt32(reader[0]);
                    punishment.Type = reader[1].ToString();
                    punishmentTypes.Add(punishment);
                }
                return punishmentTypes;
            }
            catch (Exception)
            {
                throw new Exception("Error occured during Reading Information");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public string DeletePunishType(int Id)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"Delete from tblPunishmentType where Id='" + Id + "'";
                CommandObj.ExecuteReader();
                return "Punished Type Deleted Successfully";


            }
            catch (Exception)
            {

                throw new Exception("Error occured during Deleting Punished Type");
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}